package com.hrt.util;

import com.btcode.db.result.Row;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class DataHandleUtil {

    /**
     * 过滤器，根据过滤字段、值获取数据
     *
     * @param rows
     * @param filterField
     * @param filterValue
     * @return
     */
    public static List<Row> filter(List<Row> rows, String filterField, String filterValue) {
        List<Row> result = new ArrayList<>();
        if (rows.size() > 0) {
            for (int i = 0; i < rows.size(); i++) {
                Row row = rows.get(i);
                if (filterValue.equals(row.getSafeString(filterField))) {
                    result.add(row);
                }
            }
        }
        return result;
    }

    public static List<Row> filter(List<Row> rows, String filterField, Date filterValue) {
        List<Row> result = new ArrayList<>();
        if (rows.size() > 0) {
            for (int i = 0; i < rows.size(); i++) {
                Row row = rows.get(i);
                if (filterValue.compareTo(row.getDate(filterField))==0) {
                    result.add(row);
                }
            }
        }
        return result;
    }

    /**
     * 保留几位小数
     *
     * @param value
     * @param decimals
     * @return
     */
    public static Double formatDouble(Double value, int decimals) {
        BigDecimal decimal = new BigDecimal(value);
        value = decimal.setScale(decimals, BigDecimal.ROUND_HALF_UP).doubleValue();
        return value;
    }

    /**
     * 判断日期是否同一天
     *
     * @param day1
     * @param day2
     * @return
     */
    public static boolean isSameDay(Date day1, Date day2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String ds1 = sdf.format(day1);
        String ds2 = sdf.format(day2);
        if (ds1.equals(ds2)) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 生成map
     *
     * @param rows      数据
     * @param uniqueKey 唯一的key
     * @return Map
     */
    public static <T extends Row> Map<String, T> toMap(List<T> rows, String uniqueKey) {
        Map<String, T> map = new LinkedHashMap<>();
        for (T row : rows) {
            String siteId = row.getSafeString(uniqueKey);
            map.put(siteId, row);
        }
        return map;
    }

    /**
     * 生成set，常用语用记录中的ID生成set
     *
     * @param rows 数据
     * @param key  唯一的key
     * @return Set
     */
    public static Set<String> toSet(List<Row> rows, String key) {
        Set<String> set = new HashSet<>();
        for (Row row : rows) {
            String value = row.getSafeString(key);
            set.add(value);
        }
        return set;
    }

    public static Map<String, String> jsonToMap(String jsonStr) {
        if (StringUtils.hasText(jsonStr)) {
            Gson gson = new Gson();
            TypeToken<Map<String, String>> typeToken = new TypeToken<Map<String, String>>() {
            };
            return gson.fromJson(jsonStr, typeToken.getType());
        }
        return new HashMap<>();
    }

    /**
     * 生成set，常用语用记录中的ID生成set
     *
     * @param rows 数据
     * @param key  唯一的key
     * @return Set
     */
    public static <MapObject extends Map> Map<String, List<MapObject>> toListMap(List<MapObject> rows, String key) {
        Map<String, List<MapObject>> map = new HashMap<>();
        for (MapObject row : rows) {
            String value = toNonNullString(row.get(key));
            if (!map.containsKey(value)) {
                map.put(value, new ArrayList<>());
            }
            map.get(value).add(row);
        }
        return map;
    }

    public static String toSqlString(Collection<String> list) {
        StringBuilder groupIdsBuilder = new StringBuilder();
        for (String groupId : list) {
            groupIdsBuilder.append(",");
            groupIdsBuilder.append("'");
            groupIdsBuilder.append(groupId);
            groupIdsBuilder.append("' ");
        }
        return groupIdsBuilder.toString().replaceFirst(",", "");
    }

    public static boolean findFromTreeList(List<Row> list, Traversal<Row> traversal) {
        boolean find = false;//标识是否找到
        for (Row row : list) {
            find = traversal.traversal(row);
            if (find) {
                break;
            }
            Object children = row.get("children");
            if (children instanceof List) {
                List<Row> childList = (List<Row>) children;
                find = findFromTreeList(childList, traversal);
                if (find) {
                    break;
                }
            }
        }
        return find;
    }

    private static String toNonNullString(Object o) {
        if (o == null) {
            return "";
        }
        else if (o instanceof String) {
            return (String) o;
        }
        else {
            return o.toString();
        }
    }

    public interface Traversal<Item> {
        boolean traversal(Item item);
    }
}
